<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2019-04-08
 * Time: 15:14
 */

namespace app\service;


use app\model\LoginstatusModel;
use app\common\model\User as UserModel;

use think\Exception;

/**
 * 登陆
 * Class LoginService
 * @package app\service
 */
class LoginService extends BaseService
{
    protected $loginStatusModel;
    public function __construct()
    {
        $this->loginStatusModel=new LoginstatusModel();//M('loginstatus');
    }

    /**
     * @param $where
     * @return array|bool|false|\PDOStatement|string|\think\Model
     */
    public function findLoginStatus($where){
        try{
            if(empty($where)){
                return false;
            }
            return $this->loginStatusModel->where($where)->find();
        }catch (Exception $ex){
            return false;
        }

    }

    /**
     * 更新用户登录状态
     * @param $where
     * @return bool
     */
    public function updateStatus($where){
        if(empty($where)){
            return false;
        }
        $save['expire_time']=time()+7*24*3600;
        $save['session_token'] = createPassword(uniqid().time().$where['uid']);
        $re=$this->loginStatusModel->where($where)->update($save);
        if($re===false){
            return false;
        }
        return $save['session_token'];
    }

    /**
     * @param $addInfo
     * @return bool|mixed
     */
    public function addStatus($addInfo){
        if(empty($addInfo)){
            return false;
        }
        return $this->loginStatusModel->save($addInfo);
    }

    /**
     * @param $uid
     * @return bool|string
     */
    public function checkStatus($uid){
        //查询更新登录状态维护
        $login=$this->findLoginStatus(array('uid'=>$uid));
        if($login){
            if($login['expire_time']<time()){
                $token=self::updateStatus(array('uid'=>$uid));//修改登录状态维持时间
            }else{
                $token=$login['session_token'];
            }
            LoginstatusModel::where('uid',$uid)->update(['update_time'=>time()]);
        }else{
            //新增一个登录状态
            $add['uid']=$uid;
            $info = UserModel::getOneById($uid);
            $add['mall_id']=$info['mall_id'];
            $add['expire_time']=time()+7*24*3600;
            $add['create_time']=time();
            $add['session_token'] = createPassword(uniqid().time().$uid);
            $re=self::addStatus($add);
            if($re){
                $token=$add['session_token'];
            }else{
                return false;
            }
        }
        return $token;
    }

    /**
     * 检查token返回uid
     * @param $token
     * @return bool
     */
    public function checkTokenBackUid($token){
        //查询更新登录状态维护
        $login=$this->findLoginStatus(array('session_token'=>$token));
        if(!$login){
            return false;
        }else{
            LoginstatusModel::where('uid',$login['uid'])->update(['update_time'=>time()]);
            if($login['expire_time']<time()){
                return false;
            }else{
                return $login['uid'];
            }
        }
    }


}